﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using MpSoft.Library.BUSS.Categories;
using MpSoft.Library.BUSS.Products;
using MpSoft.Library.DTO;
using MpSoft.Basic.Utils;

namespace MpSoft.Web.Controls.Product
{
    public partial class ProductsControl : System.Web.UI.UserControl
    {
      
        #region Var

        private string seName = string.Empty;
        private Library.DTO.Category category = null;

        #endregion

        #region Method

        protected string GetCategory()
        {

            var list = CategoryBUSS.Category_ByParentID(0, "san-pham", null, 1);
            if (list != null)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<ul class='list-product'>");
                foreach (var obj2 in list)
                    if (!obj2.SeName.Equals("san-pham"))
                        sb.Append("<li><a href='" + SEOHelper.GetCategoryUrl(obj2.SeName) + "'>" + obj2.CategoryName + "</a><p>" + GeSubCategory(obj2.CategoryID) + "</p></li>");
                sb.Append("</ul>");
                return sb.ToString();
            }
            return "";
        }

        private string GeSubCategory(int parentid)
        {
            var list = CategoryBUSS.Category_Total("san-pham", parentid, 1);
            if (list != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var obj in list)
                    sb.Append("<a href='" + SEOHelper.GetCategoryUrl(obj.SeName) + "'>" + obj.CategoryName + "(" + obj.TotalPro + ")</a>");
                return sb.ToString();
            }
            return "";
        }

        protected string GetCategoy(string categoryid)
        {
            if (!string.IsNullOrEmpty(categoryid))
            {
                var list = GetParent(int.Parse(categoryid));
                if (list.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = list.Count - 1; i > -1; i--)
                        sb.Append("<a href='" + SEOHelper.GetCategoryUrl(list[i].SeName) + "'>" + list[i].CategoryName + "</a>");
                    return sb.ToString();
                }
            }
            return "";
        }

        private List<Category> GetParent(int parentid)
        {
            var obj = CategoryBUSS.Category_GetByID(parentid);
            List<Category> list = new List<Category>();
            while (obj != null)
            {
                list.Add(obj);
                obj = CategoryBUSS.Category_GetByID(obj.ParentID);
            }
            return list;
        }

        private void BindControl()
        { 
            if(category==null)
                category = CategoryBUSS.Category_BySeName(seName);
        }

        private void LoadProduct(int pageindex)
        {
            BindControl();
            if (!this.seName.Equals("san-pham"))
            {
                int TotalRow;
                MpPage.PageSize = 15;
                var list = ProductBUSS.Product_Paged(category.CategoryID, 1, 1, null, pageindex, MpPage.PageSize, out TotalRow);
                rpList.DataSource = list;
                rpList.DataBind();
                MpPage.ItemCount = TotalRow;
            }
        }

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["CategoryCode"]))
                seName = Request.QueryString["CategoryCode"].ToString().Trim();
            LoadProduct(1);
        }


        protected void MpPage_Command(object sender, CommandEventArgs e)
        {

            int pageindex;
            int.TryParse(e.CommandArgument.ToString(), out pageindex);
            MpPage.CurrentIndex = pageindex;
            LoadProduct(pageindex);

        }

        #endregion

        #region Properties

        public string SeName { get { return seName; } }

        #endregion
    }
}